package com.healthcloud.service.impl;



import java.util.*;

import com.healthcloud.dao.SysUserDao;
import com.healthcloud.entity.SysRoleEntity;
import com.healthcloud.entity.SysUserEntity;
import com.healthcloud.service.SysRoleService;
import com.healthcloud.service.SysUserRoleService;
import com.healthcloud.service.SysUserService;
import com.healthcloud.utils.Constant;
import com.healthcloud.utils.EncryptionUtils;
import com.healthcloud.utils.PageParam;
import com.healthcloud.utils.RRException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



/**
 * 系统用户
 * Created by yu on 2017/6/6.
 */
@Service("sysUserService")
@Transactional
public class SysUserServiceImpl implements SysUserService {
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysUserRoleService sysUserRoleService;
	@Autowired
	private SysRoleService sysRoleService;


	public List<SysUserEntity> search(SysUserEntity entity, PageParam pageParam) {

		return sysUserDao.queryList(entity,pageParam);
	}

	public List<String> queryAllPerms(SysUserEntity entity) {
		return sysUserDao.queryAllPerms(entity);
	}


	public List<Long> queryAllMenuId(Long userId) {
		return sysUserDao.queryAllMenuId(userId);
	}


	public SysUserEntity queryByUserName(String username) {
		return sysUserDao.queryByUserName(username);
	}

	@Override
	public SysUserEntity getByOpenId(String openId) {
		return sysUserDao.getByOpenId(openId);
	}

	@Override
	public SysUserEntity queryByMobile(String mobile) {
		return sysUserDao.queryByMobil(mobile);
	}
	@Override
	public SysUserEntity queryByEmail(String email) {
		return sysUserDao.queryByEmail(email);
	}

	public SysUserEntity queryObjectByid(Long userId) {
		return sysUserDao.queryObject(userId);
	}


	public List<SysUserEntity> queryList(SysUserEntity entity, PageParam pageParam){
		return sysUserDao.queryList(entity,pageParam);
	}


	public int queryTotal(SysUserEntity entity) {
		return sysUserDao.queryTotal(entity);
	}

	public void save(SysUserEntity user) {
		user.setCreateTime(new Date());
		user.setPassword(EncryptionUtils.encryptionMd5(user.getPassword()));
		sysUserDao.save(user);

		//检查角色是否越权
//		checkRole(user);

		//保存用户与角色关系
		if(user.getRoleList() != null) {
			List<Long> roleIdList = new ArrayList<Long>();
			for (SysRoleEntity role : user.getRoleList()) {
				if(role.getRoleId() != null) {
					roleIdList.add(role.getRoleId());
				}
			}

			sysUserRoleService.saveOrUpdate(user.getId(), roleIdList);
		}

	}


	public void update(SysUserEntity user) {
		if(StringUtils.isBlank(user.getPassword())){
			SysUserEntity _user = sysUserDao.queryObject(user.getId());
			user.setPassword(_user.getPassword());
		}else{
			user.setPassword(EncryptionUtils.encryptionMd5(user.getPassword()));
		}
		sysUserDao.update(user);

		//检查角色是否越权
//		checkRole(user);

		//保存用户与角色关系
		if(user.getRoleList() != null) {
			List<Long> roleIdList = new ArrayList<Long>();
			for (SysRoleEntity role : user.getRoleList()) {
				if(role.getRoleId() != null) {
					roleIdList.add(role.getRoleId());
				}
			}
			sysUserRoleService.saveOrUpdate(user.getId(), roleIdList);
		}
	}


	public void deleteBatch(Long[] userId) {
		sysUserDao.deleteBatch(userId);
	}


	public int updatePassword(Long userId, String password, String newPassword) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userId", userId);
		map.put("password", password);
		map.put("newPassword", newPassword);
		return sysUserDao.updatePassword(map);
	}

	/**
	 * 检查角色是否越权
	 */
	private void checkRole(SysUserEntity user){

		//如果不是超级管理员，则需要判断用户的角色是否自己创建
		if(user.getUsername().equals(Constant.ADMIN)){
			return ;
		}

		//查询用户创建的角色列表
		List<Long> roleIdList = sysRoleService.queryRoleIdList(user.getCreateUserId());

		//判断是否越权
		List<Long> _roleIdList = new ArrayList<Long>();
		if(user.getRoleList() != null) {
			for (SysRoleEntity role : user.getRoleList()) {
				if(role.getRoleId() != null) {
					_roleIdList.add(role.getRoleId());
				}
			}
			if (!roleIdList.containsAll(_roleIdList)) {
				throw new RRException("新增用户所选角色，不是本人创建");
			}
		}
	}
}
